//
//  CameraView.swift
//  ShiZhe
//
//  Created by 陈卿伟 on 16/9/6.
//  Copyright © 2016年 陈卿伟. All rights reserved.
//

import UIKit

class CameraView: UIView {
    var bgView:UIView!
    var cameraButton:UIButton!//使用相机button
    var albumButton:UIButton!//使用相册button
    var cancelButton:UIButton!//取消按钮
    override init(frame: CGRect) {
        bgView = UIView()
        cameraButton = UIButton()
        albumButton = UIButton()
        cancelButton = UIButton()
        super.init(frame: frame)
        self.backgroundColor = UIColor.clearColor()
        self.alphaBlackBackG()
        self.createBGView()
        self.createCameraButton()
        self.createAlbumButton()
        self.createCancelButton()
        self.animateForBGView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

//MARK:--UI
extension CameraView{
    
    //创建透明黑色背景
    func alphaBlackBackG(){
        let blackView = UIView(frame: CGRect(x: 0,y: 0,width: WidthAPP,height: HeightAPP))
        blackView.backgroundColor = UIColor.blackColor()
        blackView.alpha = 0.3
        self.addSubview(blackView)
    }
    
    
    //创建175大小的底部view
    func createBGView(){
        bgView.frame = CGRect(x: 0, y: HeightAPP, width: WidthAPP, height: 175*SCALEH)
        bgView.backgroundColor = Theme.GrayColor
        self.addSubview(bgView)
    }
    
    
    //创建使用相机button
    func createCameraButton(){
        cameraButton.frame = CGRect(x: 0, y: 0, width: WidthAPP, height: 55*SCALEH)
        cameraButton.backgroundColor = BaseFunc.createCustomColor(241, b: 241, g: 241, a: 1)
        cameraButton.setTitle("使用相机", forState: UIControlState())
        cameraButton.setTitleColor(UIColor.blackColor(), forState: UIControlState())
        cameraButton.titleLabel?.font = BaseFunc.getCustomFont(13)
        bgView.addSubview(cameraButton)
    }
    
    //创建使用相册button
    func createAlbumButton(){
        albumButton.frame = CGRect(x: 0, y: 56*SCALEH, width: WidthAPP, height: 55*SCALEH)
        albumButton.backgroundColor = BaseFunc.createCustomColor(241, b: 241, g: 241, a: 1)
        albumButton.setTitle("使用相册", forState: UIControlState())
        albumButton.setTitleColor(UIColor.blackColor(), forState: UIControlState())
        albumButton.titleLabel?.font = BaseFunc.getCustomFont(13)
        bgView.addSubview(albumButton)
    }
    
    
    //创建取消button
    func createCancelButton(){
        cancelButton.frame = CGRect(x: 0, y: 120*SCALEH, width: WidthAPP, height: 55*SCALEH)
        cancelButton.backgroundColor = BaseFunc.createCustomColor(241, b: 241, g: 241, a: 1)
        cancelButton.setTitle("取消", forState: UIControlState())
        cancelButton.setTitleColor(UIColor.blackColor(), forState: UIControlState())
        cancelButton.titleLabel?.font = BaseFunc.getCustomFont(13)
        cancelButton.addTarget(self, action: #selector(self.cancelButtonClick(_:)), forControlEvents: .TouchUpInside)
        bgView.addSubview(cancelButton)
    }
    
    //将bgview从下面升上来
    func animateForBGView(){
        UIView.animateWithDuration(0.2, delay: 0, options: .CurveEaseOut, animations: {
            self.bgView.frame.origin.y -= 175*SCALEH
            }, completion: nil)
    }
}


//MARK:--点击事件
extension CameraView{
    //取消按钮点击事件
    func cancelButtonClick(sender:UIButton){
        print("点击取消")
        animateForCameraView()
    }
    
    
    //收回动画
    func animateForCameraView(){
        UIView.animateWithDuration(0.2, delay: 0, options: .CurveEaseOut, animations: {
            self.bgView.frame.origin.y += (175*SCALEH)
        }) { (end) in
            self.removeFromSuperview()
        }
    }
}











